{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## パッケージでクラスをまとめる\n",
    "\n",
    "* パッケージ\n",
    "    * 関連のあるクラスをまとめる仕組み\n",
    "    * もしクラスが数千個あってもパッケージとしてまとめることで一桁減らしてクラス管理できる\n",
    "    * インターネットのドメイン名をパッケージ名として使う事で世界で唯一の名前が重複しないクラスを作ることができる\n",
    "        * <font color=\"red\">ドメイン名を逆にして階層構造にする規則がある</font>\n",
    "    * パッケージ名とフォルダの階層構造を一致させる\n",
    "        * フォルダの階層構造をパッケージお通りにつくる\n",
    "        * その中にクラスを配置する\n",
    "    * パッケージの指定\n",
    "        * ソースコードの最初に記述する\n",
    "    * コンパイル\n",
    "        * ディレクトリを含めた<font color=\"red\">ファイル名</font>で指定する\n",
    "        * 例\n",
    "            * `javac net/pgtop/test/PersonTest.java`\n",
    "    * 実行\n",
    "        * パッケージ名を含めた<font color=\"red\">クラス名</font>で指定\n",
    "        * 例\n",
    "            * `java net.pgtop.test.PersonTest`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "%%bash\n",
    "### パッケージ用ディレクトリの作成\n",
    "mkdir -p com/github/ftakao2007/test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "name=Person\n",
    "\n",
    "### ソースの編集\n",
    "### パッケージ用の階層にファイルを作成\n",
    "cat <<- EOS > com/github/ftakao2007/test/${name}.java\n",
    "\n",
    "//# パッケージの指定\n",
    "package com.github.ftakao2007.test;\n",
    "\n",
    "public class ${name} {\n",
    "  private String name;\n",
    "  private int age;\n",
    "  private String address;\n",
    "  \n",
    "  public Person(String _name, int _age, String _address){\n",
    "    name = _name;\n",
    "    age = _age;\n",
    "    address = _address;\n",
    "  }\n",
    "  \n",
    "  public void say(){\n",
    "    System.out.println(\"Name:\" + name + \" Age:\" + age + \" Address:\" + address);\n",
    "  }\n",
    "  \n",
    "  public void setName(String _name){\n",
    "    name = _name;\n",
    "  }\n",
    "  \n",
    "  public void setAge(int _age){\n",
    "    if (_age > 0) {\n",
    "      age = _age;\n",
    "    }else{\n",
    "      age = 0;\n",
    "    }\n",
    "  }\n",
    "  \n",
    "  public void setAddress(String _address){\n",
    "    address = _address;\n",
    "  }\n",
    "  \n",
    "  public String getName(){\n",
    "    return name;\n",
    "  }\n",
    "  \n",
    "  public int getAge(){\n",
    "    return age;\n",
    "  }\n",
    "  \n",
    "  public String getAddress(){\n",
    "    return address;\n",
    "  }\n",
    "}\n",
    "EOS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name:Taro Age:21 Address:Tokyo\n",
      "Name:Hanako Age:18 Address:Hokkaido\n",
      "Jiro 19 Osaka\n",
      "Hanako 18 Hokkaido\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "name=PersonTest\n",
    "\n",
    "\n",
    "### ソースの編集\n",
    "### パッケージ用の階層にファイルを作成\n",
    "cat <<- EOS > com/github/ftakao2007/test/${name}.java\n",
    "\n",
    "//# パッケージの指定\n",
    "package com.github.ftakao2007.test;\n",
    "\n",
    "public class ${name} {\n",
    "  public static void main (String[] args) {\n",
    "\n",
    "    Person[] people = new Person[2];\n",
    "    people[0] = new Person(\"Taro\", 21, \"Tokyo\");\n",
    "    people[1] = new Person(\"Hanako\", 18, \"Hokkaido\");\n",
    "\n",
    "    for(int i = 0; i < people.length; i++){\n",
    "      people[i].say();\n",
    "    }\n",
    "\n",
    "    people[0].setName(\"Jiro\");\n",
    "    people[0].setAge(19);\n",
    "    people[0].setAddress(\"Osaka\");\n",
    "\n",
    "    for(int i = 0; i < people.length; i++){\n",
    "      System.out.println(people[i].getName() + \" \" + people[i].getAge() + \" \" + people[i].getAddress());\n",
    "    }\n",
    "\n",
    "  }\n",
    "}\n",
    "EOS\n",
    "\n",
    "### コンパイル\n",
    "#javac ${name}.java\n",
    "javac -encoding UTF-8 com/github/ftakao2007/test/${name}.java\n",
    "\n",
    "### 実行\n",
    "java com.github.ftakao2007.test.${name}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Javaの異なるパッケージにあるクラスを使う\n",
    "\n",
    "* 階層構造\n",
    "    * 前回\n",
    "        * com/github/ftakao2007/test/Person.java (com.github.ftakao2007.test.Personパッケージ)\n",
    "        * com/github/ftakao2007/test/PersonTest.java (com.github.ftakao2007.test.PersonTestパッケージ)\n",
    "    * 今回\n",
    "        * com/github/ftakao2007/test/Person.java (com.github.ftakao2007.test.Personパッケージ)\n",
    "        * PersonTest.java  (無名パッケージ)\n",
    "            * これら二つは別パッケージ\n",
    "* 完全修飾名でクラス名を指定する\n",
    "    * 同一パッケージの場合は完全修飾名を省略できる\n",
    "* java.langパッケージだけは例外\n",
    "    * Stringクラスはjava.lang(正式にはjava.lang.String)パッケージ\n",
    "    * 例外的に重要なクラスなのd完全修飾名にしなくても使うことができる"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "name=Person\n",
    "\n",
    "### ソースの編集\n",
    "cat <<- EOS > com/github/ftakao2007/test/${name}.java\n",
    "\n",
    "package com.github.ftakao2007.test;\n",
    "\n",
    "public class ${name} {\n",
    "  private String name;\n",
    "  private int age;\n",
    "  private String address;\n",
    "  \n",
    "  public Person(String _name, int _age, String _address){\n",
    "    name = _name;\n",
    "    age = _age;\n",
    "    address = _address;\n",
    "  }\n",
    "  \n",
    "  public void say(){\n",
    "    System.out.println(\"Name:\" + name + \" Age:\" + age + \" Address:\" + address);\n",
    "  }\n",
    "  \n",
    "  public void setName(String _name){\n",
    "    name = _name;\n",
    "  }\n",
    "  \n",
    "  public void setAge(int _age){\n",
    "    if (_age > 0) {\n",
    "      age = _age;\n",
    "    }else{\n",
    "      age = 0;\n",
    "    }\n",
    "  }\n",
    "  \n",
    "  public void setAddress(String _address){\n",
    "    address = _address;\n",
    "  }\n",
    "  \n",
    "  public String getName(){\n",
    "    return name;\n",
    "  }\n",
    "  \n",
    "  public int getAge(){\n",
    "    return age;\n",
    "  }\n",
    "  \n",
    "  public String getAddress(){\n",
    "    return address;\n",
    "  }\n",
    "}\n",
    "EOS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name:Taro Age:21 Address:Tokyo\n",
      "Name:Hanako Age:18 Address:Hokkaido\n",
      "Jiro 19 Osaka\n",
      "Hanako 18 Hokkaido\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "name=PersonTest\n",
    "\n",
    "### ソースの編集\n",
    "cat <<- EOS > ${name}.java\n",
    "\n",
    "public class ${name} {\n",
    "  public static void main (String[] args) {\n",
    "  \n",
    "    //# 完全修飾名で指定\n",
    "    com.github.ftakao2007.test.Person[] people = new com.github.ftakao2007.test.Person[2];\n",
    "    people[0] = new com.github.ftakao2007.test.Person(\"Taro\", 21, \"Tokyo\");\n",
    "    people[1] = new com.github.ftakao2007.test.Person(\"Hanako\", 18, \"Hokkaido\");\n",
    "\n",
    "    for(int i = 0; i < people.length; i++){\n",
    "      people[i].say();\n",
    "    }\n",
    "\n",
    "    people[0].setName(\"Jiro\");\n",
    "    people[0].setAge(19);\n",
    "    people[0].setAddress(\"Osaka\");\n",
    "\n",
    "    for(int i = 0; i < people.length; i++){\n",
    "      System.out.println(people[i].getName() + \" \" + people[i].getAge() + \" \" + people[i].getAddress());\n",
    "    }\n",
    "\n",
    "  }\n",
    "}\n",
    "EOS\n",
    "\n",
    "### コンパイル\n",
    "#javac ${name}.java\n",
    "javac -encoding UTF-8 ${name}.java\n",
    "\n",
    "### 実行\n",
    "java ${name}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## importでパッケージ名を省略する\n",
    "\n",
    "* import\n",
    "    * パッケージ名を省略できる\n",
    "        * 例\n",
    "            * `import com.github.ftakao2007.test.Person;`\n",
    "                * testフォルダ配下のPersonクラスを対象にする場合\n",
    "            * `import com.github.ftakao2007.test.*;`\n",
    "                * testフォルダ以下にあるクラスを全て対象にする場合\n",
    "                * .<font color=\"red\">サブフォルダは対象外</font> \n",
    "                    * サブフォルダは別パッケージ扱いになる\n",
    "                    * 例えば上の指定で `com.github.ftakao2007.test.sub` フォルダ配下にあるクラスは<font color=\"red\">importされない</font>\n",
    "    * java.langパッケージは例外的にimportしなくてもクラス名だけで使える\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "name=Person\n",
    "\n",
    "### ソースの編集\n",
    "cat <<- EOS > com/github/ftakao2007/test/${name}.java\n",
    "\n",
    "package com.github.ftakao2007.test;\n",
    "\n",
    "public class ${name} {\n",
    "  private String name;\n",
    "  private int age;\n",
    "  private String address;\n",
    "  \n",
    "  public Person(String _name, int _age, String _address){\n",
    "    name = _name;\n",
    "    age = _age;\n",
    "    address = _address;\n",
    "  }\n",
    "  \n",
    "  public void say(){\n",
    "    System.out.println(\"Name:\" + name + \" Age:\" + age + \" Address:\" + address);\n",
    "  }\n",
    "  \n",
    "  public void setName(String _name){\n",
    "    name = _name;\n",
    "  }\n",
    "  \n",
    "  public void setAge(int _age){\n",
    "    if (_age > 0) {\n",
    "      age = _age;\n",
    "    }else{\n",
    "      age = 0;\n",
    "    }\n",
    "  }\n",
    "  \n",
    "  public void setAddress(String _address){\n",
    "    address = _address;\n",
    "  }\n",
    "  \n",
    "  public String getName(){\n",
    "    return name;\n",
    "  }\n",
    "  \n",
    "  public int getAge(){\n",
    "    return age;\n",
    "  }\n",
    "  \n",
    "  public String getAddress(){\n",
    "    return address;\n",
    "  }\n",
    "}\n",
    "EOS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name:Taro Age:21 Address:Tokyo\n",
      "Name:Hanako Age:18 Address:Hokkaido\n",
      "Jiro 19 Osaka\n",
      "Hanako 18 Hokkaido\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "name=PersonTest\n",
    "\n",
    "### ソースの編集\n",
    "cat <<- EOS > ${name}.java\n",
    "\n",
    "//# パッケージをインポート\n",
    "import com.github.ftakao2007.test.Person;\n",
    "\n",
    "public class ${name} {\n",
    "  public static void main (String[] args) {\n",
    "  \n",
    "    //# importで完全修飾名で指定しなくても良くなる\n",
    "    Person[] people = new Person[2];\n",
    "    people[0] = new Person(\"Taro\", 21, \"Tokyo\");\n",
    "    people[1] = new Person(\"Hanako\", 18, \"Hokkaido\");\n",
    "\n",
    "    for(int i = 0; i < people.length; i++){\n",
    "      people[i].say();\n",
    "    }\n",
    "\n",
    "    people[0].setName(\"Jiro\");\n",
    "    people[0].setAge(19);\n",
    "    people[0].setAddress(\"Osaka\");\n",
    "\n",
    "    for(int i = 0; i < people.length; i++){\n",
    "      System.out.println(people[i].getName() + \" \" + people[i].getAge() + \" \" + people[i].getAddress());\n",
    "    }\n",
    "\n",
    "  }\n",
    "}\n",
    "EOS\n",
    "\n",
    "### コンパイル\n",
    "#javac ${name}.java\n",
    "javac -encoding UTF-8 ${name}.java\n",
    "\n",
    "### 実行\n",
    "java ${name}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
